package com.design.twords.adatper;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.design.twords.R;
import com.design.twords.activity.WordDetailActivity;
import com.design.twords.config.ConstantValue;
import com.design.twords.database.OcrWordsList;
import com.design.twords.database.Searchistory;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  22:39
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.MyViewHolder> {

    private LayoutInflater myInflater;
    private List<Searchistory> wordItem;
    private Context context;
    private int viewId;

    public SearchAdapter(List<Searchistory> wordItem, Context context, int viewId) {
        this.myInflater = LayoutInflater.from(context);
        this.wordItem = wordItem;
        this.context = context;
        this.viewId = viewId;

    }

    public void setWordItem(List<Searchistory> wordItem) {
        this.wordItem = wordItem;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(myInflater.inflate(viewId, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Searchistory item = wordItem.get(position);
        holder.word.setText(item.getWord());
        holder.trans.setText(item.getTrans());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Searchistory history = new Searchistory();
                history.setWordId(item.getWordId());
                history.setWord(item.getWord());
                history.setWordBook(item.getWordBook());
                history.setTrans(item.getTrans());
                history.save();

                //跳转到详情界面
                Intent intent = new Intent(context, WordDetailActivity.class);
                intent.putExtra("flags", ConstantValue.SEARCH_WORD);
                intent.putExtra("wordId", item.getWordId()+"");
                intent.putExtra("booktype", item.getWordBook());
                context.startActivity(intent);
            }
        });

        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OcrWordsList wordsList = new OcrWordsList();
                wordsList.setWordId(item.getWordId());
                wordsList.setWord(item.getWord());
                wordsList.setWordBook(item.getWordBook());
                wordsList.save();
                holder.add.setVisibility(View.GONE);
                Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return wordItem.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView word, trans;
        private ImageView add;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            word = itemView.findViewById(R.id.searchitem_word);
            trans = itemView.findViewById(R.id.searchitem_trans);
            add = itemView.findViewById(R.id.searchitem_add);
        }
    }
}
